package br.uniriotec.ppgi.leo.controller.nlp.srl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;

import br.uniriotec.ppgi.leo.controller.run.SystemCaller;
import br.uniriotec.ppgi.leo.model.PreprocessedSentence;


/**
 * This class is responsible for calling a Semantic Role Labeler
 * that will process a group of POS-tagged sentences.
 * 
 * @author Felipe Leao
 *
 */
public class LTHSemanticRoleLabeler {
	
	private static Logger logger = Logger.getLogger(LTHSemanticRoleLabeler.class);
	private static String LTH_SRL_ROOT = "";
	
//	private static final String SYNMODEL 	= LTH_SRL_ROOT+"models/train_at_pp_more2nd.model";
//	private static final String LM			= LTH_SRL_ROOT+"models/lm_080602_uknpreds.model";
//	private static final String GM_CD		= LTH_SRL_ROOT+"models/global_partcq_mc_cd_2o_ukp.model";
//	private static final String GM_CL		= LTH_SRL_ROOT+"models/part12345_cq_mc_2o_ukp.sv.model";
//	private static final String NSYN		= LTH_SRL_ROOT+"4";
//	private static final String NSEM		= LTH_SRL_ROOT+"4";
//	private static final String SYNW		= LTH_SRL_ROOT+"25";
//	private static final String GMW			= LTH_SRL_ROOT+"3";
//	private static final String PB_FRAMES	= LTH_SRL_ROOT+"pb_frames";
//	private static final String NB_FRAMES	= LTH_SRL_ROOT+"nb_frames";
	
	
	@SuppressWarnings("unused")
	private LTHSemanticRoleLabeler(){}
	
	public LTHSemanticRoleLabeler(String lth_srl_root){
		LTH_SRL_ROOT = lth_srl_root;
	}
	
	/**
	 * <p>Creates a temporary file formatted according to the CoNLL-2008 format.
	 * The file is a tabulated file in which each line has a word 
	 * that composes the sentence and sentences are separated by 
	 * two line breaks. The lines regarding a sentence are enumerated 
	 * and each line also carries informations such as lemma and pos-tag.
	 * </p>
	 * 
	 * @param preprocessedSentences - list of sentences with lemma and POS tags
	 * @return a fille in CoNLL-2008 format
	 * @throws IOException 
	 */
	public File tempInputFileBuilder(List<PreprocessedSentence> preprocessedSentences) throws IOException{
		String tempFileExtension = UUID.randomUUID().toString();
		
		try{
			File conll2008_file = File.createTempFile("srl_input_"+tempFileExtension, ".tmp"); //Nao deveria ser .tokens? 
			conll2008_file.deleteOnExit();
			
			FileWriter fw = new FileWriter(conll2008_file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			
			for(PreprocessedSentence ps : preprocessedSentences){
				String[] words = ps.getWords();
				String[] posTags = ps.getPosTags();
				String[] lemmas = ps.getLemmas();
				
				for(int i=0; i<ps.getWords().length; i++){
					//First columm set
					bw.write(i+1+"\t");
					bw.write(words[i]+"\t");
					bw.write(lemmas[i]+"\t");
					bw.write("_\t");
					bw.write(posTags[i]+"\t");
					
					//Second columm set
					bw.write(words[i]+"\t");
					bw.write(lemmas[i]+"\t");
					bw.write(posTags[i]+"\t");
					bw.write("0\t");
					bw.write("_\t");
					bw.write("\n");
				}
				bw.write("\n");
			}
			
			bw.close();
			return conll2008_file;
			
		}catch(IOException ex){
			logger.error("ERROR when build SRL input file. "+ex.getMessage());
			throw new IOException("ERROR when trying to create " +
					"temp file to use as input on SRL system. "+ex.getMessage());
		}
	}
	
	
	public File annotateSRL(List<PreprocessedSentence> preprocessedSentences) throws IOException{
		File inputFile = tempInputFileBuilder(preprocessedSentences);
		
		String invoke = LTH_SRL_ROOT+"run.sh < "+inputFile.getAbsolutePath() + " > LTH_SRL/leo-lth_srl.output";
		SystemCaller.callSystemProccess(invoke);
		
		
		return null;
	}

}
